﻿using System;
using System.Text.RegularExpressions;
using System.Linq;
using JetBrains.Annotations;




namespace Crocodile.SimplexLanguage.Attributes
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    [MeansImplicitUse]
    public class SimplexAutoPropertiesAttribute : Attribute
    {
        public readonly Regex Pattern;


        public SimplexAutoPropertiesAttribute() 
            : this (@"^(?<name>[\w][\w\d_]*)\s*=\s*(?<value>[^\s].*)$")
        {}


        public SimplexAutoPropertiesAttribute(string pattern)
        {
            Pattern = 
                new Regex(pattern, RegexOptions.IgnorePatternWhitespace);

            var groupNames = Pattern.GetGroupNames();
            bool ok = groupNames.Contains("name") && groupNames.Contains("value");
            if (!ok)
                throw new ArgumentException("SimplexAutoPropertiesAttribute: the pattern must contain groups 'name' and 'value'.", pattern);
        }
    }
}
